#!/bin/bash
message "check kernel soucre ...."
if [ ! -d $OPI_K_source ]
then
	message "soucre not foud !"
	exit 0
fi
export
message "entry kernel source dir " && cd $OPI_K_source

if [ ! -f .config ]
then 
	message "clear   kernel " && make distclean
	message "cpoy $BOARD config file" && cp ${OPI_WORK_DIR}/config/kernel/$BOARD  ${OPI_K_source}/arch/arm/configs/${OPI_K_config}
	message "config  $BOARD kernel " && make $OPI_K_config
fi
##build kernel
date
export KERNEL_release=`make -s kernelrelease -C ./`
message "kernel release : $KERNEL_release"
echo ""
#make -j8 uImage ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
message "Building kernel..." && make -j8 uImage
if [ $? -eq 0 ]
then	
  printf "COMPILE OK.   %-50s \r\n" 
else
  printf "COMPILE ERROR.%-50s \r\n" 
  exit 1
fi
date
echo ""

#copy file to output dir
if [ -f arch/arm/boot/zImage ];then
message "copy uImage file to $OPI_KERNEL_OUTPUT_DIR" && cp arch/arm/boot/uImage  $OPI_KERNEL_OUTPUT_DIR 
message "copy zImage file to $OPI_KERNEL_OUTPUT_DIR" && cp arch/arm/boot/zImage  $OPI_KERNEL_OUTPUT_DIR 
message "copy .config file to $OPI_KERNEL_OUTPUT_DIR" && cp .config  $OPI_KERNEL_OUTPUT_DIR/config-$KERNEL_release
message "copy System.map file to $OPI_KERNEL_OUTPUT_DIR" && cp System.map  $OPI_KERNEL_OUTPUT_DIR 
else 
message "Error: build fail !"
message "Please check source ,see $OPI_KERNEL_OUTPUT_DIR/build.log file"
exit 1
fi
echo ""
##build modules
message "Building modules..." && make -j8 modules
echo ""
##install modules
#export INSTALL_MOD_PATH=${OPI_KERNEL_OUTPUT_DIR}
#export INSTALL_HDR_PATH=${OPI_KERNEL_OUTPUT_DIR}/usr
mkdir -p ${INSTALL_HDR_PATH}
message "install headers" && make  headers_install_all INSTALL_HDR_PATH=${INSTALL_HDR_PATH}
message "install modules" && make  modules_install 
message "install firmware" && make  firmware_install
echo ""
if [$OPI_KernelSourceType == "sunxi" && $OPI_CPU == "h3"];
then
#build mali driver
message "entry mali driver source dir " && cd $OPI_K_source/modules/mali
MOD_DIR=$OPI_KERNEL_OUTPUT_DIR/lib/modules/$KERNEL_release
mkdir -p $MOD_DIR/kernel/drivers/gpu/mali
mkdir -p $MOD_DIR/kernel/drivers/gpu/ump
make KERNEL_DIR=$OPI_K_source MOD_DIR=$MOD_DIR  clean 
make KERNEL_DIR=$OPI_K_source MOD_DIR=$MOD_DIR  build
make KERNEL_DIR=$OPI_K_source MOD_DIR=$MOD_DIR  install
fi
echo ""
message "build kernel finish,Please to see $OPI_KERNEL_OUTPUT_DIR directories" 
echo "^_^"

